﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="Microsoft Corporation">
//   Microsoft Corporation.
// </copyright>
// <summary>
//   The program.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ThumbGen
{
    using System;

    using Microsoft.Test.CommandLineParsing;

    /// <summary>
    /// The program.
    /// </summary>
    internal class Program
    {
        #region Constants and Fields

        /// <summary>
        /// The original column used to reset the cursor when displaying the progres.
        /// </summary>
        private static int s_originalColumn;

        /// <summary>
        /// The original row used to reset the cursor when displaying the progres.
        /// </summary>
        private static int s_originalRow;

        #endregion

        #region Methods

        /// <summary>
        /// Main entrypoint to the application.
        /// </summary>
        /// <param name="args">
        /// The command line arguments.
        /// </param>
        private static void Main(string[] args)
        {
            ThumbGenArguments arguments = new ThumbGenArguments();
            try
            {
                arguments.ParseArguments(args);
                Console.WriteLine();
                if (arguments.I == null)
                {
                    Console.WriteLine("Input filename not specified.\n");
                    PrintUsage();
                    return;
                }

                ThumbnailGenerator thumbnailGenerator = new ThumbnailGenerator();

                SetThumbnailFormat(thumbnailGenerator, arguments.Format);

                Console.WriteLine("Analyzing...");
                thumbnailGenerator.FileName = arguments.I;
                thumbnailGenerator.OutputDirectory = arguments.O;
                thumbnailGenerator.Name = arguments.Name;

                if (arguments.Interval.HasValue)
                {
                    thumbnailGenerator.TimeInterval = TimeSpan.FromSeconds(arguments.Interval.Value);
                }

                if (arguments.Width.HasValue)
                {
                    thumbnailGenerator.Width = arguments.Width.Value;
                }

                Console.WriteLine("Extracting thumbnails from {0}", thumbnailGenerator.FileName);

                s_originalRow = Console.CursorTop;
                s_originalColumn = Console.CursorLeft;
                thumbnailGenerator.Progress += OnProgress;

                thumbnailGenerator.GenerateThumbnails();
            }
            catch (ArgumentException exp)
            {
                Console.Write("ERROR Occurred: ");
                Console.WriteLine(exp.Message);
            }
        }

        /// <summary>
        /// Progress callback used to display the current progress as a percentage.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="progress">
        /// The current progress, 0 to 1.
        /// </param>
        private static void OnProgress(object sender, ProgressEventArgs progress)
        {
            Console.SetCursorPosition(s_originalColumn, s_originalRow);
            Console.WriteLine("{0:##0.0%}", progress.Progress);
        }

        /// <summary>
        /// Print the usage of the command line app to the console.
        /// </summary>
        private static void PrintUsage()
        {
            Console.WriteLine(@"USAGE: Thumbgen -i:inputfilename.avi [options]");
            Console.WriteLine();
            Console.WriteLine(@"     -i:file :: Specifies the input video file to extract the thumbnails from.");
            Console.WriteLine(@"   -o:folder :: Specifies the output folder for the thumbnails.");
            Console.WriteLine(
                @" -interval:n :: Specifies the interval in seconds between the thumbnails (defaults to 5 seconds).");
            Console.WriteLine(@"    -width:n :: Specifies the width of each thumbnail.");
            Console.WriteLine(@" -format:xxx :: Specifies the format of the thumbnails - jpg or png.");
            Console.WriteLine(@"     -name:x :: Gets or sets the name to use as the prefix for the thumbnail.");
        }

        /// <summary>
        /// Sets the thumbnail format.
        /// </summary>
        /// <param name="thumbnailGenerator">
        /// The thumbnail generator.
        /// </param>
        /// <param name="format">
        /// The format as a string - "jpg" or "png".
        /// </param>
        /// <exception cref="ArgumentException">
        /// Thrown if an invalid type is passed.
        /// </exception>
        private static void SetThumbnailFormat(ThumbnailGenerator thumbnailGenerator, string format)
        {
            if (format != null)
            {
                if (string.Compare(format, "png", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    thumbnailGenerator.Format = Format.Png;
                }
                else if (string.Compare(format, "jpg", StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    thumbnailGenerator.Format = Format.Jpg;
                }
                else
                {
                    throw new ArgumentException(
                        "Unrecognized thumbnail format " + format + ". Please choose png or jpg.");
                }
            }
        }

        #endregion
    }
}